/*
 * 作者: 侯晓宇
 * 时间: 2017-04-05 
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */
define(function(require) {

	require("jquery");
	var appInfo = require('app/AppInfo');
	var c = require('app/utils/CommonUtils');
	var hc = require('app/utils/HttpClientUtils');
	var tip = require('app/utils/TipUtils');
	var EchoLazyUtils = require('app/utils/EchoLazyUtils');
	var RoomInfoUtils = require('app/utils/RoomInfoUtils');
	var UserInfoUtils = require('app/utils/UserInfoUtils');
	var CacheUtils = require('app/utils/CacheUtils');
	var HashMap = require('app/utils/HashMapUtils');
	var action = {};
	
	var win = window;
	var currentuserid = UserInfoUtils.getLoginedUser().userid;

	EchoLazyUtils.load();

	win.param = api.pageParam;

	win.pos = 0;
	win.container = $("#divs");
	win.class_empty_placeholder = "empty-placeholder";
	win.searchResultMap = new HashMap();
	win.usertype = "";
		
	action.setUserType = function(usertype){
		win.usertype = usertype;
	}


	action.prove = function(useruuid){
		var url = appInfo.serverbase + "/luyan/admin/user/"+useruuid+"/prove";
		var body = {};
		
		hc.post(url,body,function(resp){
			if(resp.result){
				$("#user_"+useruuid).remove();
			}else{
				tip.show("操作超时，请重试");
			}
		});
	}
	
	action.revoke = function(useruuid){
		var url = appInfo.serverbase + "/luyan/admin/user/"+useruuid+"/revoke";
		var body = {};
		
		hc.post(url,body,function(resp){
			if(resp.result){
				$("#user_"+useruuid).remove();
			}else{
				tip.show("操作超时，请重试");
			}
		});
	}
	
	action.admin_revoke = function(useruuid){
	
		api.confirm({
			title : '温馨提示',
			msg : '是否要收回该用户的管理员权限？',
			buttons : ['暂不', '收回']
		}, function(ret, err) {
			//coding...
			if (ret.buttonIndex == "2") {
				var url = appInfo.serverbase + "/luyan/admin/user/"+useruuid+"/admin_revoke";
				var body = {};
				
				hc.post(url,body,function(resp){
					if(resp.result){
						$("#user_"+useruuid).remove();
					}else{
						tip.show("操作超时，请重试");
					}
				});
			}
		});
	

	}

	action.fetchInHeaderMode = function() {
		win.pos = 0;
		var url = appInfo.serverbase + "/luyan/users";
		var body = {
			refreshtype: "head", //all, head, foot
			pos: win.pos,
			usertype: win.usertype,
			size: 20
		};
		hc.post(url, body, function(resp) {
			if (resp.status != "200") {
				api.refreshHeaderLoadDone();
				return;
			}
			api.refreshHeaderLoadDone();
			
			c.l(resp);
			var data = resp.result;
			win.pos = resp.result.pos;
			if (data.users.length == 0) {
				action.getNoResult();
			} else {
				win.container.html("");
				action.getResult(data);
			}

		});

	}

	action.fetchInFooterMode = function() {
		var url = appInfo.serverbase + "/luyan/users";
		var body = {
			refreshtype: "footer",
			pos: win.pos,
			usertype: win.usertype,
			size: 20
		};
		hc.post(url, body, function(resp) {
			if (resp.status != "200") {
				return;
			}
			c.l(resp);
			var data = resp.result;
			win.pos = resp.result.pos;
			if (data.users.length == 0) {
				api.toast({
	                msg:'下面没有了'
                });
			} else {
				action.getResult(data);
			}

		});
	}

	action.getResult = function(data) {
		var users = data.users;
		var divs = win.container;
		$("." + win.class_empty_placeholder).remove();
		users.forEach(function(item) {
			win.searchResultMap.put(item.uuid,item);
			//头像是否使用了缓存，如果头像地址为null，则用app中的默认图片，不使用缓存
			var avatar_use_cache = false;
			
			var btndiv = '<div class="aui-btn aui-btn-info">通过审核</div>';
			if(c.isNullStr(win.usertype)){
				btndiv = '<div class="aui-btn aui-btn-success">通过审核</div>';
			}else if(win.usertype == "1"){
				btndiv = '<div class="aui-btn aui-btn-danger">撤销授权</div>';
			}else if(win.usertype == "9"){
				btndiv = '<div class="aui-btn aui-btn-danger">收回管理员权限</div>';
			}
			
			if(currentuserid == item.uuid){
				btndiv = '';
			}
			
			var div = $(
			'<li class="aui-list-item aui-list-item-middle user_item" data-unionid="'+item.unionid+'" id="user_'+item.uuid+'">'+
	            '<div class="aui-media-list-item-inner">'+
	                '<div class="aui-list-item-media" style="width: 3rem;height:3rem;">'+
	                    '<img src="../image/user_avatar_default.jpg" data-echo="'+c.handleUrl(item.wxHeadimgurl)+'" class="aui-img-round aui-list-img-sm">'+
	                '</div>'+
	                '<div class="aui-list-item-inner">'+
	                    '<div class="aui-list-item-text">'+
	                        '<div class="aui-list-item-title aui-font-size-14">'+item.name+'</div>'+
	                        '<div class="aui-list-item-right">'+
                    			btndiv +
                			'</div>'+
	                    '</div>'+
	                    '<div class="aui-list-item-text" style="font-size:0.6rem;">'+
	                        '手机：'+ item.tel +
	                    '</div>'+
	                    '<div class="aui-list-item-text"  style="font-size:0.6rem;">'+
	                        '最近访问：'+ c.timeFormat(item.lastlogin) +
	                    '</div>'+
	                '</div>'+
	            '</div>'+
	        '</li>');
	        
	        divs.append(div);
	        

	        if(currentuserid != item.uuid){
		        $('#user_'+item.uuid).click(function(){
		        	if(c.isNullStr(win.usertype)){
		        		action.prove(item.uuid);
		        	}else if(win.usertype == "1"){
		        		action.revoke(item.uuid);
		        	}else if(win.usertype == "9"){
		        		action.admin_revoke(item.uuid);
		        	}
		        });	        
	        }

		});

		EchoLazyUtils.load();
		api.parseTapmode();
	}
	action.getNoResult = function() {
		var div = $("<div class=\"" + win.class_empty_placeholder + "\"></div>");
		div.css({
			width: "100%",
			"font-size": "0.8rem",
			color: "#666",
			"text-align": "center"
		});
		div.html("空空如也……");
		win.container.html("");
		win.container.append(div);
	}
	

	return action;
});